package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import com.lljjcoder.style.citylist.Toast.ToastUtils;

import androidx.fragment.app.DialogFragment;
import app.fugema.shop.databinding.ActivityUserMoneyBinding;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ItemUserMoneyRecordViewBinding;
import app.fugema.shop.http.SuccessResult;
import app.fugema.shop.main.center.model.BalanceDetailResult;
import app.fugema.shop.main.center.model.BalanceResult;
import app.fugema.shop.main.center.vm.UserVM;
import app.fugema.shop.main.order.view.ShoppingConfirmOrderActivity;
import app.fugema.shop.util.passwardview.FinishInputPassword;
import app.fugema.shop.util.passwardview.PassWordAgainFragment;

/**
 * 用户资产
 */
public class UserMoneyActivity extends BaseTitleActivity<ActivityUserMoneyBinding, UserVM> implements OnGetDataListener
{
    private String userKey;

    private SingleRecyclerAdapter<BalanceDetailResult.RowsBean, ItemUserMoneyRecordViewBinding> adapter;

    private DialogFragment pwdDialog;

    public static void start(Context context)
    {
        Intent intent = new Intent(context, UserMoneyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_money);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("我的资产",R.color.white);

        adapter = new SingleRecyclerAdapter<BalanceDetailResult.RowsBean, ItemUserMoneyRecordViewBinding>()
        {
            @Override
            public int getLayoutId()
            {
                return R.layout.item_user_money_record_view;
            }

            @Override
            public void onBindViewHolder(ItemUserMoneyRecordViewBinding binding, int position, BalanceDetailResult.RowsBean bean)
            {
                super.onBindViewHolder(binding, position, bean);
                StringBuffer tvTitle = new StringBuffer();
                // 订单类型
                switch (bean.getSourceType())
                {
                    case 1:
                        //未知
                        break;
                    case 2:
                        tvTitle.append("充值");
                        break;
                    case 3:
                        //未知
                        break;
                    case 4:
                        tvTitle.append("消费");
                        break;
                    case 5:
                        tvTitle.append("退款");
                        break;
                }
                tvTitle.append("(");
                tvTitle.append(bean.getSourceData());
                tvTitle.append(")");
                binding.tvRecordTitle.setText(tvTitle.toString());

                StringBuffer moenySb = new StringBuffer();
                if(bean.getAmount() > 0)
                {
                    moenySb.append("+");
                    moenySb.append(bean.getAmount());
                }
                else
                {
                    moenySb.append(bean.getAmount());
                }
                binding.tvAmount.setText( moenySb.toString() );
            }
        };
        //充值
        binding.btnCharge.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                ChargeMoneyActivity.start(UserMoneyActivity.this);
            }
        });
        //提现
        binding.btnTixian.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                // 先查询 是否设置 支付密码
                setCheckPayPwd();
            }
        });
        binding.rvRecyclerView.setAdapter(adapter);
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
    }


    @Override
    protected void initData(Intent intent)
    {
        userKey = LoginUtils.getToken();
        getUserMoney();
        onGetData(1);
    }

    public void getUserMoney()
    {
        viewModel.getUserMoney(userKey).observe(this, new BaseViewObserver<LiveDataWrapper<BalanceResult>>() {
            @Override
            protected void onSuccess(LiveDataWrapper<BalanceResult> data)
            {
                binding.setItem(data.data);
                binding.executePendingBindings();
            }
            @Override
            public boolean isEmptyData(LiveDataWrapper<BalanceResult> data)
            {
                return  UIUtils.isEmpty( data.data);
            }
        });
    }

    @Override
    public void onGetData(int pn)
    {
        viewModel.getUserMoneyDetail(pn,userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<BalanceDetailResult>>(viewUtils.getStateLayout())
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<BalanceDetailResult> data)
                     {
                         viewUtils.setDataListRefreshLayout(adapter, pn, 15,data.data.getRows());
                     }

                     @Override
                     public boolean isEmptyData(LiveDataWrapper<BalanceDetailResult> data)
                     {
                         return pn == 1 && UIUtils.isEmpty(data.data.getRows());
                     }
                     @Override
                     public boolean isRefreshLayoutData()
                     {
                         return true;
                     }
                 });

    }

    private void setCheckPayPwd()
    {
        //先查询 是否设置 支付密码
        viewModel.isSetPayPwd(userKey).observe(this, new BaseViewObserver<LiveDataWrapper<SuccessResult>>() {
            @Override
            protected void onSuccess(LiveDataWrapper<SuccessResult> data)
            {
                if( data.data.isSuccess() )
                {
                    //有支付密码
                    TiXianActivity.start(UserMoneyActivity.this);
                }
                else
                {
                    //没设置密码 就设置密码
                    pwdDialog = PassWordAgainFragment.newInstace(new FinishInputPassword() {
                        @Override
                        public void finishPassword(String password)
                        {
                            viewModel.setPayPwd(password, userKey)
                                    .observe(UserMoneyActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>() {
                                        @Override
                                        protected void onSuccess(LiveDataWrapper<BaseApiResult> data) {
                                            //{"success":true,"msg":"设置成功", "code":0}
                                            if (data.data.isSuccess())
                                            {
                                                ToastUtils.showShortToast(UserMoneyActivity.this, "设置成功");
                                                TiXianActivity.start(UserMoneyActivity.this);
                                            }
                                        }
                                    });
                        }

                        @Override
                        public void clickForgetPassword()
                        {
                        }
                    }, () -> {
                        ToastUtils.showShortToast(UserMoneyActivity.this, "输入的密码不一致");
                    },()->{
                        //关闭 密码框时

                    });
                    addFragment( pwdDialog );
                }
            }
            @Override
            public boolean isEmptyData(LiveDataWrapper<SuccessResult> data)
            {
                return UIUtils.isEmpty(data.data);
            }
        });
    }
}
